package cn.kgc.ssm.controller;

import cn.kgc.ssm.commons.ResponseResult;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.form.LoginForm;
import cn.kgc.ssm.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/5/8
 * @description
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;


    // 用户分页数据
    @RequestMapping("page")
    public ResponseResult  userPage(Integer page,Integer limit,String nickname){
        System.out.println(nickname);
        return userService.userPage(page,limit,nickname);
    }

    // 用户认证
    @RequestMapping("login")
    public ResponseResult  userLogin(HttpServletRequest request, LoginForm loginForm){
        return userService.login(request,loginForm);
    }

    // 个人信息修改
    @RequestMapping("personal/edit")
    public ResponseResult  editPersonalInfo(User user){

        return userService.editPersonalInfo(user);
    }

    // 个人密码修改
    @RequestMapping("personal/password")
    public ResponseResult  editPersonalPassword(String oldPassword,String password,HttpServletRequest request){

        return userService.editPersonalPassword(oldPassword,password,request);
    }

    // 获取用户个人信息
    @RequestMapping("personalInfo")
    public ResponseResult  personalInfo(String username){

        return userService.getPersonalInfo(username);
    }


    // 添加用户信息
    @RequestMapping("add")
    public ResponseResult  addUser(User user){
        return userService.addUser(user);
    }


    // 根据用户id查询用户信息用户信息
    @RequestMapping("getById")
    public ResponseResult  getByUserId(Integer id){
        return userService.getByUserId(id);
    }

    // 根据用户id查询用户信息用户信息
    @RequestMapping("edit")
    public ResponseResult  editUser(User user){
        return userService.editUser(user);
    }


    //删除用户信息
    @RequestMapping("delete")
    public ResponseResult  deleteUser(@RequestBody List<Integer> ids){

        return userService.removeUser(ids);
    }

    //重置密码
    @RequestMapping("resetPassword")
    public ResponseResult  resetPassword(Integer id){

        return userService.resetPassword(id);
    }

    // 退出登录
    @RequestMapping("logout")
    public ResponseResult  logout(HttpServletRequest request){

        HttpSession session = request.getSession();
        // 清空session
        session.invalidate();
        return ResponseResult.success().message("安全退出");
    }
}
